﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Google.KML;
using MSAProject.AltitudeColor;
namespace MSAProject
{
    class KmlGenerator
    {
        private List<gePolygon> _polygonList = new List<gePolygon>();
        private string _filepath;
        private string _docName = "Default Name";
        private AltitudeColorGenerator _altitudeColorGenerator;
        public KmlGenerator(string filePath, string docName)
        {
            _filepath = filePath;
            _docName = docName;
            _altitudeColorGenerator = new AltitudeColorGenerator(3000);
            _altitudeColorGenerator.Alpha = 255;
        }

        public void AddPolygon(TerrainField terrainField)
        {
            var coordinates = new List<geCoordinates> { terrainField.TopLeft, terrainField.TopRight, terrainField.BottomRight, terrainField.BottomLeft, terrainField.TopLeft };
                
            var linearRing = new geLinearRing(coordinates);
            var outerBoundary = new geOuterBoundaryIs(linearRing);
            var msaSquare = new gePolygon(outerBoundary) {Extrude = true, AltitudeMode = geAltitudeModeEnum.absolute};

            _polygonList.Add(msaSquare);
        }

        public void GenerateKmlFile()
        {
            geDocument doc = new geDocument();
            doc.Name = _docName;
            
            for (int i = 0; i < _polygonList.Count; i++ )
            {
                var pm = new gePlacemark();
                pm.Geometry = _polygonList[i];
                _altitudeColorGenerator.AssignAltitude(pm);
                doc.Features.Add(pm);
            }

            _altitudeColorGenerator.AddStylesToKMLDoc(doc);
            geKML kml = new geKML(doc);
            File.WriteAllBytes(_filepath, kml.ToKML());
        }

    }
}
